
#include <cstdlib>
#include <iostream>

#include "core/server/Controller.h"
#include "core/common/Device.h"
#include "network/Server.h"

int main(int argc, char *argv[])
{
    // Vérification des paramètres.
    if(argc > 2)
    {
        std::cerr << "Usage : server [port]" << std::endl;
        return EXIT_FAILURE;
    }

    Server server;
    server.listen(argc == 1 ? 31337 : atoi(argv[1]));

    Device device(&server);

    std::cout << "> Concurrent control server is running..." << std::endl;
    while(true)
    {
        Controller* controller = new Controller(&server, &device);
        if(!controller->connect())
        {
            std::cout << controller->socketError() << " : " << controller->errorString() << std::endl;
        }
        else
        {
            std::cout << "> " << controller->interface() << " has connected" << std::endl;
            controller->exec();
        }
    }

    return EXIT_SUCCESS;
}
